/**
 * 
 */
package cn.crm.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Service;
import cn.crm.bean.AddressBook;
import cn.crm.dao.AddressBookDao;
import cn.crm.service.AddressBookService;

/**
 * @author wbh
 * 
 */
@Service("addressBookService")
public class AddressBookServiceImpl implements AddressBookService {
	@Resource
	private AddressBookDao bookDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.service.AddressBookService#save(cn.crm.bean.AddressBook)
	 */
	@Override
	public boolean save(AddressBook book) {
		book.setAbIsdelete(false);
		return bookDao.save(book) != null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.service.AddressBookService#upd(cn.crm.bean.AddressBook)
	 */
	@Override
	public boolean upd(AddressBook book) {
		// TODO Auto-generated method stub
		book.setAbIsdelete(false);
		return bookDao.upd(book) > 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.service.AddressBookService#del(cn.crm.bean.AddressBook)
	 */
	@Override
	public boolean del(AddressBook book) {
		// TODO Auto-generated method stub
		return bookDao.del(book) > 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.service.AddressBookService#showByTj(cn.crm.bean.AddressBook)
	 */
	@Override
	public List<AddressBook> show(Integer pagesize, Integer currpage) {
		// TODO Auto-generated method stub
		return bookDao.show(pagesize, currpage);
	}

	/**
	 * @param bookDao
	 *            the bookDao to set
	 */
	public void setBookDao(AddressBookDao bookDao) {
		this.bookDao = bookDao;
	}

	@Override
	public AddressBook get(String id) {
		// TODO Auto-generated method stub
		return bookDao.get(id);
	}

	@Override
	public String search(AddressBook book) {
		// TODO Auto-generated method stub
		JSONArray array = new JSONArray();
		List<AddressBook> list = bookDao.showByName(book);
		for (AddressBook b : list) {
			JSONObject object = new JSONObject();
			object.put("abProid", b.getAbProid());
			object.put("customer", b.getCustomer().getCustName());
			object.put("user", b.getUser().getUsEmpname());
			object.put("ddlDepartement", b.getDDLByTypeDepartments()
					.getTypeValue());
			object.put("ddlDutiesname", b.getDDLByTypeDutiesname()
					.getTypeValue());
			object.put("ddlSex", b.getDDLByTypeSex().getTypeValue());
			object.put("abTellphone", b.getAbTellphone());
			object.put("abZipcode", b.getAbZipcode());
			object.put("abEmail", b.getAbEmail());
			object.put("abQq", b.getAbQq());
			object.put("abProname", b.getAbProname());
			object.put("abInterest", b.getAbInterest());
			object.put("abBirth", b.getAbBirth().toString());
			object.put("abAddress", b.getAbAddress());
			array.add(object);
		}
		
		return array.toJSONString();
	}

	@Override
	public List<AddressBook> excel(AddressBook book) {
		// TODO Auto-generated method stub
		return bookDao.showByName(book);
	}

	@Override
	public Integer page(Integer pagesize) {
		// TODO Auto-generated method stub
		Integer count = bookDao.count();
		return count % pagesize == 0 ? count / pagesize : count / pagesize + 1;
	}

	@Override
	public JSONArray showChange(AddressBook book,Integer pagesize, Integer currpage) {
		JSONArray array = new JSONArray();
		List<AddressBook> list = bookDao.showChange(book, pagesize, currpage);
		for (AddressBook b : list) {
			JSONObject object = new JSONObject();
			object.put("abProid", b.getAbProid());
			object.put("customer", b.getCustomer().getCustName());
			object.put("user", b.getUser().getUsEmpname());
			object.put("ddlDepartement", b.getDDLByTypeDepartments()
					.getTypeValue());
			object.put("ddlDutiesname", b.getDDLByTypeDutiesname()
					.getTypeValue());
			object.put("ddlSex", b.getDDLByTypeSex().getTypeValue());
			object.put("abTellphone", b.getAbTellphone());
			object.put("abZipcode", b.getAbZipcode());
			object.put("abEmail", b.getAbEmail());
			object.put("abQq", b.getAbQq());
			object.put("abProname", b.getAbProname());
			object.put("abInterest", b.getAbInterest());
			object.put("abBirth", b.getAbBirth().toString());
			object.put("abAddress", b.getAbAddress());
			array.add(object);
		}
		
		return array;
	}
}
